package com.lanyan.system.controller.api;

import com.lanyan.common.annotation.RepeatSubmit;

import java.math.BigDecimal;

import com.lanyan.common.core.domain.*;
import com.lanyan.common.utils.SecurityUtils;
import com.lanyan.system.domain.Courier;
import com.lanyan.system.domain.vo.CourierVo;
import com.lanyan.system.service.ICourierService;
import io.swagger.annotations.Api;

import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyan.common.annotation.Log;
import io.swagger.annotations.ApiOperation;
import com.lanyan.common.core.controller.BaseController;
import com.lanyan.common.core.domain.R;
import com.lanyan.common.enums.BusinessType;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import springfox.documentation.annotations.ApiIgnore;
import com.lanyan.common.utils.StringUtils;
import com.lanyan.system.domain.Evaluate;
import com.lanyan.system.domain.vo.EvaluateVo;
import com.lanyan.system.service.IEvaluateService;
import com.lanyan.common.utils.poi.ExcelUtil;
import com.lanyan.common.core.page.TableDataInfo;

/**
 * 评价Controller
 *
 * @author lanyan
 * @date 2024-11-26
 */
@RestController
@Api(tags = {"评价"})
@RequestMapping("/api/evaluate")
public class ApiEvaluateController extends BaseController {
    @Autowired
    private IEvaluateService evaluateService;
    @Autowired
    private ICourierService courierService;

//    /**
//     * 查询评价列表
//     */
//    @GetMapping("/page")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "int", dataTypeClass = int.class),
//            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", dataTypeClass = int.class),
//    })
//    @ApiOperation("查询评价列表")
//    public PageR<EvaluateVo> list(@ApiIgnore Evaluate evaluate) {
//        startPage();
//        evaluate.setAccountId(SecurityUtils.getUserId());
//        List<EvaluateVo> list = evaluateService.selectEvaluateVoList(evaluate);
//        return PageR.page(list);
//    }

    /**
     * 获取评价详细信息
     */
//    @GetMapping(value = "/info")
//    @ApiOperation("获取评价详细信息")
//    public R<Evaluate> getInfo(Long evaluateId) {
//        return R.ok(evaluateService.selectEvaluateByEvaluateId(evaluateId));
//    }

    /**
     * 新增评价
     */
    @Log(title = "评价", businessType = BusinessType.INSERT)
    @PostMapping("save")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId", value = "所属订单", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "courierId", value = "所属配送员,只有评价配送员的时候传", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "productId", value = "所属商品", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "speed", value = "速度评分", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "manner", value = "服务评分/商品评价", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "distribution", value = "配送评分/描述相符", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "content", value = "评价内容", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "image", value = "图片", dataType = "String", dataTypeClass = String.class),
    })
    @RepeatSubmit
    @ApiOperation("新增评价")
    public R<Evaluate> add(@ApiIgnore @RequestBody Evaluate evaluate) {
        StringUtils.insertRequired(evaluate);
        evaluate.setAccountId(SecurityUtils.getUserId());
        evaluateService.insertEvaluate(evaluate);
        if (evaluate.getCourierId() != null){
            Courier courier = courierService.getById(evaluate.getCourierId());
            BigDecimal sum = new BigDecimal(evaluate.getSpeed()).add(new BigDecimal(evaluate.getManner())).add(new BigDecimal(evaluate.getDistribution()));
            BigDecimal decimal = sum.divide(BigDecimal.valueOf(3), 2, BigDecimal.ROUND_HALF_UP);
            courier.setEvaluateSum(BigDecimal.valueOf(courier.getEvaluateSum()).add(decimal).longValue());
            courier.setEvaluateTotal(courier.getEvaluateTotal() + 1);
            evaluateService.lambdaUpdate()
                    .eq(EvaluateVo::getEvaluateId,evaluate.getEvaluateId())
                    .set(BaseEntity::getRemark,decimal)
                    .update();
            courierService.lambdaUpdate()
                    .eq(CourierVo::getCourierId,courier.getCourierId())
                    .set(CourierVo::getEvaluateSum,courier.getEvaluateSum())
                    .set(CourierVo::getEvaluateTotal,courier.getEvaluateTotal())
                    .update();
        }
        return R.ok(evaluate);
    }

//    /**
//     * 修改评价
//     */
//    @Log(title = "评价", businessType = BusinessType.UPDATE)
//    @PostMapping("update")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
//            @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
//            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "accountId", value = "所属用户", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "ordersId", value = "所属订单", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "courierId", value = "所属配送员", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "productId", value = "所属商品", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "speed", value = "速度评分", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "manner", value = "服务评分/商品评价", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "distribution", value = "配送评分/描述相符", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "content", value = "评价内容", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "image", value = "图片", dataType = "String", dataTypeClass = String.class),
//    })
//    @RepeatSubmit
//    @ApiOperation("修改评价")
//    public AjaxResult edit(@ApiIgnore @RequestBody Evaluate evaluate) {
//        StringUtils.updateRequired(evaluate);
//        evaluateService.updateEvaluate(evaluate);
//        return success("修改成功");
//    }
//
//    /**
//     * 删除评价
//     */
//    @Log(title = "评价", businessType = BusinessType.DELETE)
//    @GetMapping("/del")
//    @ApiOperation("删除评价")
//    public AjaxResult remove(Long evaluateId) {
//        evaluateService.deleteEvaluateByEvaluateId(evaluateId);
//        return success("删除成功");
//    }
}
